package com.xju.admin.service.Impl;

import com.xju.admin.entity.*;
import com.xju.admin.mapper.DictionaryMapper;
import com.xju.admin.mapper.UserMapper;
import com.xju.admin.service.DictionaryService;
import com.xju.admin.service.UserService;
import com.xju.admin.utils.DateUtil;
import com.xju.admin.utils.IDGenerator;
import com.xju.admin.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DictionaryService dictionaryService;

    @Override
    public User findById(String id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(User user) {
        userMapper.insert(user);
    }

    @Override
    public User findByName(String name) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserNameEqualTo(name);
        return userMapper.selectByExampleWithBLOBs(userExample).get(0);
    }

    @Override
    public User findByEmail(String email) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserEmailEqualTo(email);
        return userMapper.selectByExample(userExample).get(0);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public UserResult findAll() {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserIdIsNotNull().andUserNameNotEqualTo("admin");
        List<User> userList=userMapper.selectByExampleWithBLOBs(userExample);
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        List<UserShow> userShowsList=new ArrayList<>();
        for (User user:userList){
            UserShow userShow=new UserShow(user);
            String status=user.getUserStatus();
            Date loginTime=user.getUserLoginTime();
            String newStatus=dictionaryService.findById(status).getDicValue();
            userShow.setNewStatus(newStatus);
            if (loginTime!=null){
                userShow.setNewLoginTime(DateUtil.dateFormat(loginTime));
            }else{
                userShow.setNewLoginTime("");
            }
            userShowsList.add(userShow);
        }
        List<Dictionary> dictionaryList=dictionaryService.findByType("用户状态");
        UserResult userResult=new UserResult(userShowsList,dictionaryList);
        return userResult;
    }

    @Override
    public void delete(String id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void modify(String id, String name, String email, String password, String status, String profile) {
        User user=userMapper.selectByPrimaryKey(id);
        if (!name.equals("")){
            user.setUserName(name);
        }
        if(!email.equals("")){
            user.setUserEmail(email);
        }
        if(!password.equals("")){
            user.setUserPassword(password);
        }
        if (!status.equals("")){
            user.setUserStatus(status);
        }
        if (!profile.equals("")){
            user.setUserProfile(profile);
        }
        userMapper.updateByPrimaryKeyWithBLOBs(user);
    }

    @Override
    public void addUser(String name, String status, String email, String password, String profile) {
        User user=new User();
        user.setUserId(IDGenerator.getId());
        user.setUserName(name);
        user.setUserEmail(email);
        user.setUserPassword(PasswordUtil.encrypt(password));
        user.setUserStatus(status);
        user.setUserLoginTime(new Date());
        user.setUserProfile(profile);
        userMapper.insert(user);
    }

    @Override
    public void saveProfile(String id, String name, String password, String profile) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andUserIdEqualTo(id);
        User user=userMapper.selectByExampleWithBLOBs(userExample).get(0);
        if(password!=null){
            String newpassword=PasswordUtil.encrypt(password);
            user.setUserPassword(newpassword);
        }
        user.setUserName(name);
        user.setUserProfile(profile);
        userMapper.updateByPrimaryKeyWithBLOBs(user);
    }
}
